<!DOCTYPE html>
<head>
    <title>socket demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<input type="text" id="msg" />
<button id="send">发送</button>
</body>
</html>
<script>
    $(function()
    {
        // 创建socket对象
        var socket = new WebSocket('ws://'+document.domain+':9501');

        // 打开Socket
        socket.onopen = function(event)
        {

            $('#send').on('click', function()
            {
                var msg = $('#msg').val();
                socket.send(msg);
                console.log(msg);
            });
            // 发送一个初始化消息
            socket.send("客户端初始化！");

            // 监听消息
            socket.onmessage = function(event) {
                console.log("客户端监听到的消息：", event);
            };

            // 监听Socket的关闭
            socket.onclose = function(event) {
                console.log("Socket关闭了", event);
            };

            // 关闭Socket....
            //socket.close()
        };
    });
</script>